package offer;

import java.util.Stack;

public class Offer31 {


//    class Solution {
//        public boolean validateStackSequences(int[] pushed, int[] popped) {
//            Stack<Integer> stack = new Stack<>();
//            int j = 0;
//            for(int i = 0;i<pushed.length;i++){
//                stack.push(pushed[i]);
//                while(!stack.isEmpty() &&stack.peek() == popped[j] ){
//                    j++;
//                    stack.pop();
//                }
//            }
//            return stack.isEmpty();
//        }
//    }

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack = new Stack<>();
        int j=0;
        for (int i = 0; i < pushed.length ; i++) {
            stack.push(pushed[i]);
            while (!stack.isEmpty()&&stack.peek()==popped[j]){
                j++;
                stack.pop();
            }
        }
        return   stack.isEmpty();
    }


}
